// pages/shop/shop.ts

interface Category {
  id: number;
  name: string;
  icon: string;
  iconColor: string;
}

interface ShopProduct {
  id: number;
  name: string;
  image: string;
  price: number;
  originalPrice: number;
  tags: string[];
  sales: number;
  categoryId: number;
  // 以下是可选属性，商品列表页不一定需要显示
  description?: string;
  discount?: number;
  stock?: number;
  positiveRate?: string;
  rating?: number;
  reviewCount?: number;
  badges?: string[];
  specs?: Array<{id: number; name: string; price: number; stock: number}>;
  promotions?: Array<{type: string; description: string}>;
  images?: string[];
  features?: Array<{icon: string; title: string; description: string}>;
  params?: Array<{name: string; value: string}>;
  detailContent?: string;
  reviews?: Array<{
    id: number;
    username: string;
    avatar: string;
    rating: number;
    time: string;
    content: string;
    images: string[];
    spec: string;
  }>;
}

interface Banner {
  id: number;
  image: string;
  url: string;
}

Page({
  data: {
    searchValue: '',
    categoryIndex: 0,
    cartCount: 8,
    categories: [
      { id: 0, name: '全部', icon: 'app', iconColor: '#0052d9' },
      { id: 1, name: '中药材', icon: 'root-list', iconColor: '#ed7b2f' },
      { id: 2, name: '康养器具', icon: 'heart', iconColor: '#00a870' },
      { id: 3, name: '药膳食材', icon: 'shop', iconColor: '#0052d9' },
      { id: 4, name: '康养书籍', icon: 'book-open', iconColor: '#ed7b2f' },
      { id: 5, name: '保健用品', icon: 'flag', iconColor: '#00a870' }
    ] as Category[],
    
    products: [] as ShopProduct[],
    filteredProducts: [] as ShopProduct[],
    
    banners: [
      { id: 1, image: 'https://images.unsplash.com/photo-1591189863430-ab87e7f8e8dd', url: '/pages/product_detail/product_detail?id=1' },
      { id: 2, image: 'https://images.unsplash.com/photo-1509057095372-ddd219144499', url: '/pages/product_detail/product_detail?id=3' },
      { id: 3, image: 'https://images.unsplash.com/photo-1491329963831-6b5a2a15c402', url: '/pages/product_detail/product_detail?id=6' }
    ] as Banner[]
  },

  onLoad: function () {
    this.loadProducts();
  },

  // 加载商品数据
  loadProducts: function () {
    wx.showLoading({
      title: '加载中',
    });
    
    // 模拟API请求获取商品数据
    setTimeout(() => {
      const products: ShopProduct[] = [
        // 中药材
        {
          id: 1,
          name: '贵州苗药足浴包（10袋装）',
          image: 'https://images.unsplash.com/photo-1591189863430-ab87e7f8e8dd',
          price: 128.00,
          originalPrice: 168.00,
          tags: ['清热解毒', '舒缓疲劳'],
          sales: 589,
          categoryId: 1
        },
        {
          id: 2,
          name: '苗药草本精油',
          image: 'https://images.unsplash.com/photo-1547793548-7a0e7dfdb24f',
          price: 98.00,
          originalPrice: 108.00,
          tags: ['舒缓压力', '改善睡眠'],
          sales: 432,
          categoryId: 1
        },
        {
          id: 3,
          name: '贵州野生石斛（50g装）',
          image: 'https://images.unsplash.com/photo-1509057095372-ddd219144499',
          price: 158.00,
          originalPrice: 198.00,
          tags: ['滋阴润肺', '提高免疫'],
          sales: 245,
          categoryId: 1
        },
        
        // 康养器具
        {
          id: 4,
          name: '全自动中药煎煮壶',
          image: 'https://images.unsplash.com/photo-1564466809058-bf4114d55352',
          price: 368.00,
          originalPrice: 399.00,
          tags: ['智能操控', '一键煎煮'],
          sales: 128,
          categoryId: 2
        },
        {
          id: 5,
          name: '负离子香薰机',
          image: 'https://images.unsplash.com/photo-1606376031898-0494a6ef3ea4',
          price: 258.00,
          originalPrice: 299.00,
          tags: ['负离子', '清新空气'],
          sales: 325,
          categoryId: 2
        },
        
        // 药膳食材
        {
          id: 6,
          name: '五指山野生灵芝',
          image: 'https://images.unsplash.com/photo-1491329963831-6b5a2a15c402',
          price: 268.00,
          originalPrice: 299.00,
          tags: ['增强免疫', '养生佳品'],
          sales: 178,
          categoryId: 3
        },
        {
          id: 7,
          name: '苗家有机黑糯米',
          image: 'https://images.unsplash.com/photo-1586347367097-9164c8f4f30e',
          price: 38.00,
          originalPrice: 45.00,
          tags: ['有机', '营养丰富'],
          sales: 672,
          categoryId: 3
        },
        
        // 康养书籍
        {
          id: 8,
          name: '《贵州苗医养生古方》',
          image: 'https://images.unsplash.com/photo-1544947950-fa07a98d237f',
          price: 58.00,
          originalPrice: 68.00,
          tags: ['古方', '养生智慧'],
          sales: 95,
          categoryId: 4
        },
        
        // 保健用品
        {
          id: 9,
          name: '竹炭颈椎枕',
          image: 'https://images.unsplash.com/photo-1550581190-9c1c48d21d6c',
          price: 128.00,
          originalPrice: 158.00,
          tags: ['护颈', '改善睡眠'],
          sales: 256,
          categoryId: 5
        },
        {
          id: 10,
          name: '电加热远红外腰带',
          image: 'https://images.unsplash.com/photo-1598224815226-8f04f32d6c6c',
          price: 198.00,
          originalPrice: 238.00,
          tags: ['缓解腰痛', '远红外'],
          sales: 183,
          categoryId: 5
        }
      ];
      
      this.setData({
        products,
        filteredProducts: products
      });
      
      wx.hideLoading();
    }, 500);
  },

  // 搜索框值变化
  onSearchChange: function (e: any) {
    this.setData({
      searchValue: e.detail.value
    });
    this.filterProducts();
  },

  // 清除搜索内容
  clearSearch: function () {
    this.setData({
      searchValue: ''
    });
    this.filterProducts();
  },

  // 切换分类
  changeCategory: function (e: any) {
    const index = e.currentTarget.dataset.index;
    this.setData({
      categoryIndex: index
    });
    this.filterProducts();
  },

  // 筛选商品
  filterProducts: function () {
    const { products, searchValue, categoryIndex } = this.data;
    const selectedCategoryId = this.data.categories[categoryIndex].id;
    
    let filtered = products;
    
    // 根据分类筛选
    if (selectedCategoryId !== 0) {
      filtered = filtered.filter(item => item.categoryId === selectedCategoryId);
    }
    
    // 根据搜索关键词筛选
    if (searchValue) {
      const keyword = searchValue.toLowerCase();
      filtered = filtered.filter(item => 
        item.name.toLowerCase().includes(keyword) || 
        item.tags.some(tag => tag.toLowerCase().includes(keyword))
      );
    }
    
    this.setData({
      filteredProducts: filtered
    });
  },

  // 查看商品详情
  viewProductDetail: function (e: any) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/product_detail/product_detail?id=${id}`
    });
  },

  // 查看banner详情
  viewBanner: function (e: any) {
    const url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url
    });
  },

  // 查看购物车
  viewCart: function () {
    wx.navigateTo({
      url: '/pages/cart/cart'
    });
  }
}) 